﻿using System;
using System.Collections.Generic;
using libsharp;
using libexcel;

namespace libwutong
{
    class MyPrice
    {
        List<string> _areas = new List<string>();
        List<float> _prices = new List<float>();
        List<float> _price2s = new List<float>();

        MyCity _city = new MyCity();
        TxtPrice _txtPrice = new TxtPrice();
        TxtAreaSupple _txtAreaSupple = new TxtAreaSupple();


        public void print()
        {
            Console.WriteLine(" 检查价格表:");
            for (int i = 0; i < _areas.Count; ++i)
            {
                if(i % 2 == 0)
                {
                    Console.Write(" ");
                }
                Console.Write("{0}\t{1}\t{2}", _areas[i], _prices[i], _price2s[i]);
                if(i % 2 == 1)
                {
                    Console.WriteLine("");
                }
                else
                {
                    Console.Write("\t\t");
                }
            }
            Console.WriteLine("");
        }

        public int Index(string area, string areaOld)
        {
            bool isEmpty = areaOld.Length == 0;
            for (int i = 0; i < _areas.Count; ++i)
            {
                if (area.IndexOf(_areas[i]) >= 0)
                {
//                     if (!isEmpty)
//                     {
//                         Console.WriteLine(" " + areaOld + " ---> " + _areas[i]);
//                     }
                    return i;
                }
            }
            return -1;
        }

        public int Index(string area)
        {
            int i = Index(area, "");
            if(i >= 0)
            {
                return i;
            }
            
            
            string areaOld = area;
            area = _txtAreaSupple.Province(areaOld);
            if (area != null)
            {
                i = Index(area, areaOld);
                if (i >= 0)
                {
                    return i;
                }
            }

            area = _city.Province(areaOld);
            if (area != null)
            {
                i = Index(area, areaOld);
                if (i >= 0)
                {
                    return i;
                }
            }
            return -1;
        }

        public string Province(int i)
        {
  
            return _areas[i];
        }

        public float Price(int i)
        {
            return _prices[i];
        }

        public float Price2(int i)
        {
            return _price2s[i];
        }

        public float Price(int i, int weight)
        {
            if (weight <= _txtPrice.Threshold)
            {
                return Price(i) * weight;
            }
            else  if(_txtPrice.BillingType == 0)
            {
                return Price2(i) * weight;
            }
            else
            {
                return Price(i) * _txtPrice.Threshold + Price2(i) * (weight - _txtPrice.Threshold);
            }
        }

        public MyPrice()
        {
            System.Data.DataTable table = ExcelHelper.getTable(MyTxt.FolderConDition + "价格表.xlsx");
            if(table.Columns.Count < 3)
            {
                Console.WriteLine("{0}解析失败: 列数<3", MyTxt.FolderConDition + "价格表.xlsx");
                ConsoleSharp.exitByAnykey();
            }
            for(int k = 0; k < table.Rows.Count; ++k)
            {
                _areas.Add(table.Rows[k][0].ToString());
                _prices.Add(Convert.ToSingle(table.Rows[k][1]));
                _price2s.Add(Convert.ToSingle(table.Rows[k][2]));
            }
        }
    }
}
